import { ACTIVELIST, CHAT_GROUP, PPTACTIVEINFO, PPTSIGN, PRESIGN } from "./api"
import Axios from "../utils/axios/Axios"
import { qRCodeSign, locationSign, generalSign } from "./core"
//  查询单个课程的签到状态
async function aPromise(course: CourseType, uf: string, _d: string, UID: string, vc3: string): Promise<string | Activity> {
  const res = await Axios.request({
    url: ACTIVELIST.URL + `?fid=0&courseId=${course.courseId}&classId=${course.classId}&_=${new Date().getTime()}`,
    method: ACTIVELIST.METHOD,
    headers: {
      'Cookie': `uf=${uf}; _d=${_d}; UID=${UID}; vc3=${vc3};`,
    }
  })
  const data = res.data as any
  if (data !== null) {
    if (data.activeList && data.activeList.length != 0) {
      let otherId = Number(data.activeList[0].otherId)
      // 判断是否有效签到活动
      if ((otherId >= 0 && otherId <= 5) && data.activeList[0].status == 1) {
        // 活动开始超过一小时则忽略
        if ((new Date().getTime() - data.activeList[0].startTime) / 1000 < 3600) {
          return {
            aid: data.activeList[0].id,
            name: data.activeList[0].nameOne,
            courseId: course.courseId,
            classId: course.classId,
            otherId
          }
        }
      }
    }
    return "No Available"
  }
  return "Axios.request Fail"
}

const preSign = async (uf: string, _d: string, vc3: string, activeId: string | number, classId: string, courseId: string, uid: string) => {
  await Axios.request({
    url: PRESIGN.URL + `?courseId=${courseId}&classId=${classId}&activePrimaryId=${activeId}&general=1&sys=1&ls=1&appType=15&&tid=&uid=${uid}&ut=s`,
    method: PRESIGN.METHOD as any,
    headers: {
      'Cookie': `uf=${uf}; _d=${_d}; UID=${uid}; vc3=${vc3};`
    }
  })
}

const activityAll = async (params:UserCookieType,name:string,activity: Activity, config: { enc?: string, lnglat?: string, address?: string }) => {
  await preSign(params.uf!, params._d!, params.vc3!, activity.aid, activity.classId, activity.courseId, params._uid!);
  let msg: string | undefined = '没有签到'
  switch (activity.otherId) {

    case 4: {
      // 位置签到
      let lnglat = config.address ?? '118.59931,39.21084'
      let address = config.address ?? '华北理工大学'
      msg = await locationSign(params.uf!, params._d!, params.vc3!, name, address, activity.aid, params._uid!, lnglat.substring(lnglat.indexOf(',') + 1, lnglat.length), lnglat.substring(0, lnglat.indexOf(',')), params.fid);
      break;
    }
    case 3: {
      // 手势签到
      msg = await generalSign(params.uf!, params._d!, params.vc3!, name, activity.aid, params._uid!, params.fid)
      break;
    }
    case 5: {
      // 签到码签到
      msg = await generalSign(params.uf!, params._d!, params.vc3!, name, activity.aid, params._uid!, params.fid)
      break;
    }
    case 0: {
      let photo = false
      if (photo) {
        // 拍照签到
        // 获取照片objectId
        // let objectId = await getObjectIdFromcxPan(params.uf, params._d, params.vc3, params._uid)
        // await PhotoSign(params.uf, params._d, params.vc3, name, activity.aid, params._uid, params.fid, objectId)
      } else {
        // 普通签到
        msg = await generalSign(params.uf!, params._d!, params.vc3!, name, activity.aid, params._uid!, params.fid)
      }
      break;
    }
  }
  if (!msg) {
    return "签到失败"
  }
  return msg;
}


export default {
  aPromise,
  activityAll
}
